package com.example.apprendrechinois.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.Resources;

import com.example.apprendrechinois.R;
import com.example.apprendrechinois.model.language.Consonant;
import com.example.apprendrechinois.model.language.Language;
import com.example.apprendrechinois.model.language.Pinyin;
import com.example.apprendrechinois.model.language.Vowel;

public class ModelFactory {
	
	
	private static Language language;
	private static Context mContext;
	
	public static void setContext(Context ctx){
		mContext = ctx;
	}	
	
	public static Language getLanguage(){
		if(language == null){
			language = parseLanguage();
		}
		return language;
	}

	private static Language parseLanguage() {
		Resources res = mContext.getResources();
		
		Language l = new Language();
    	
    	try {
    		XmlPullParser xmlParser = res.getXml(R.xml.language);
			
			int eventType = xmlParser.getEventType();
			while(eventType != XmlPullParser.END_DOCUMENT){
				if(eventType == XmlPullParser.START_TAG) {
					if(xmlParser.getName().equals("language")){
						parseNodeLanguage(xmlParser, l);
					}
	            }
	            eventType = xmlParser.next();
			}			
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return l;
	}

	private static void parseNodeLanguage(XmlPullParser xmlParser, Language l) throws XmlPullParserException, IOException {
		int eventType = xmlParser.next();
		while(eventType != XmlPullParser.END_DOCUMENT){
			switch(eventType){
			case XmlPullParser.START_TAG:
				if(xmlParser.getName().equals("consonants")){
					parseNodeConsonants(xmlParser, l);
				}
				else if(xmlParser.getName().equals("vowels")){
					parseNodeVowels(xmlParser, l);
				}
				else if(xmlParser.getName().equals("compositions")){
					parseNodeCompositions(xmlParser, l);
				}
				break;
			}
            eventType = xmlParser.next();
		}		
		
	}

	private static void parseNodeConsonants(XmlPullParser xmlParser, Language l) throws XmlPullParserException, IOException {
		int eventType = xmlParser.next();
		boolean inConsonantNode = false;
		String consonantId = "";
		while(eventType != XmlPullParser.END_DOCUMENT){
			switch(eventType){
			case XmlPullParser.START_TAG:
				if(xmlParser.getName().equals("consonant")){
					inConsonantNode = true;
					consonantId = xmlParser.getAttributeValue(null, "id");
				}
				break;
			case XmlPullParser.TEXT:
				if(inConsonantNode){
					l.consonants.add(
							new Consonant(
									consonantId,
									xmlParser.getText()
							)
					);					
				}
				break;
			case XmlPullParser.END_TAG:
				if(xmlParser.getName().equals("consonant")){
					inConsonantNode = false;
				}
				else if(xmlParser.getName().equals("consonants")){
					return;
				}
				break;				
            }
            eventType = xmlParser.next();
		}
	}

	private static void parseNodeVowels(XmlPullParser xmlParser, Language l) throws XmlPullParserException, IOException {
		int eventType = xmlParser.next();
		while(eventType != XmlPullParser.END_DOCUMENT){
			switch(eventType){
			case XmlPullParser.START_TAG:
				if(xmlParser.getName().equals("vowel")){
					l.vowels.add(
							new Vowel(
									xmlParser.getAttributeValue(null, "id"),
									xmlParser.getAttributeValue(null, "prononciation"),
									xmlParser.getAttributeValue(null, "solo")
							)
					);
				}
				break;
			case XmlPullParser.END_TAG:
				if(xmlParser.getName().equals("vowels")){
					return;
				}
				break;				
            }
            eventType = xmlParser.next();
		}		
	}

	private static void parseNodeCompositions(XmlPullParser xmlParser,
			Language l) throws XmlPullParserException, IOException {
	
	}
	
	

}
